<template>
  <div class="login-container">
    <div class="login-card">
      <h2 class="title">{{ isLogin ? '登录' : '注册' }}</h2>
      
      <div class="form-group">
        <label>用户名</label>
        <InputText v-model="form.username" class="custom-input" placeholder="请输入用户名" />
      </div>
      
      <div class="form-group">
        <label>密码</label>
        <Password v-model="form.password" class="custom-input" placeholder="请输入密码" :feedback="false" toggleMask />
      </div>

      <div v-if="!isLogin" class="form-group">
        <label>确认密码</label>
        <Password v-model="form.confirmPassword" class="custom-input" placeholder="请再次输入密码" :feedback="false" toggleMask />
      </div>

      <div class="form-group remember-me">
        <Checkbox v-model="form.rememberMe" :binary="true" />
        <label>记住密码</label>
      </div>

      <div class="form-group">
        <Button :label="isLogin ? '登录' : '注册'" class="submit-button" @click="handleSubmit" />
      </div>

      <div class="switch-mode">
        <span @click="toggleMode">{{ isLogin ? '没有账号？立即注册' : '已有账号？立即登录' }}</span>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import InputText from 'primevue/inputtext'
import Password from 'primevue/password'
import Button from 'primevue/button'
import Checkbox from 'primevue/checkbox'

const router = useRouter()
const isLogin = ref(true)

const form = reactive({
  username: '',
  password: '',
  confirmPassword: '',
  rememberMe: false
})

const toggleMode = () => {
  isLogin.value = !isLogin.value
  form.confirmPassword = ''
}

const handleSubmit = () => {
  if (isLogin.value) {
    // 登录逻辑
    if (form.username && form.password) {
      if (form.rememberMe) {
        localStorage.setItem('username', form.username)
        localStorage.setItem('password', form.password)
      }
      router.push('/flower-manager')
    }
  } else {
    // 注册逻辑
    if (form.password !== form.confirmPassword) {
      alert('两次输入的密码不一致')
      return
    }
    if (form.username && form.password) {
      // 这里可以添加注册API调用
      alert('注册成功，请登录')
      isLogin.value = true
    }
  }
}

// 检查是否有记住的密码
const checkRememberedCredentials = () => {
  const savedUsername = localStorage.getItem('username')
  const savedPassword = localStorage.getItem('password')
  if (savedUsername && savedPassword) {
    form.username = savedUsername
    form.password = savedPassword
    form.rememberMe = true
  }
}

checkRememberedCredentials()
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.login-card {
  background: white;
  padding: 2rem;
  border-radius: 12px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
}

.title {
  text-align: center;
  color: #2c3e50;
  margin-bottom: 2rem;
  font-size: 1.8rem;
}

.form-group {
  margin-bottom: 1.5rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  color: #4a5568;
  font-weight: 500;
}

.custom-input {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid #e2e8f0;
  border-radius: 6px;
  transition: all 0.3s ease;
}

.custom-input:hover,
.custom-input:focus {
  border-color: #4299e1;
  box-shadow: 0 0 0 3px rgba(66, 153, 225, 0.1);
}

.remember-me {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.submit-button {
  width: 100%;
  padding: 0.75rem;
  font-weight: 500;
  transition: all 0.3s ease;
}

.switch-mode {
  text-align: center;
  margin-top: 1rem;
  color: #4299e1;
  cursor: pointer;
}

.switch-mode:hover {
  text-decoration: underline;
}
</style> 